package com.jiliang.common.result;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.io.Serializable;

/**
 * @Explain
 * @CreateTime 2017/10/14 16:03
 * @Author xumqc
 */
public class WebResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private String code;
    private String msg = "";
    private String exception = "";
    private T data;
    /** 总记录数 */
    private long total;
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
    private Long timestamp = System.currentTimeMillis();

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getException() {
        return exception;
    }

    public void setException(String exception) {
        this.exception = exception;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public static <T> WebResult<T> success() {
        WebResult<T> entity = new WebResult<T>();
        entity.setCode(Errors.SUCCESS.code);
        entity.setMsg(Errors.SUCCESS.msg);
        entity.setException(Errors.SUCCESS.exception);
        return entity;
    }

    public static <T> WebResult<T> success(T data) {
        WebResult<T> entity = new WebResult<T>();
        entity.setCode(Errors.SUCCESS.code);
        entity.setMsg(Errors.SUCCESS.msg);
        entity.setException(Errors.SUCCESS.exception);
        entity.setData(data);
        return entity;
    }

    public static <T> WebResult<T> success(T data, long total) {
        WebResult<T> entity = new WebResult<T>();
        entity.setCode(Errors.SUCCESS.code);
        entity.setMsg(Errors.SUCCESS.msg);
        entity.setException(Errors.SUCCESS.exception);
        entity.setData(data);
        entity.setTotal(total);
        return entity;
    }

    public static <T> WebResult<T> success(String msg, T data) {
        WebResult<T> entity = new WebResult<T>();
        entity.setCode(Errors.SUCCESS.code);
        entity.setData(data);
        entity.setMsg(msg);
        return entity;
    }

    public static <T> WebResult<T> fail(Errors errors) {
        WebResult<T> entity = new WebResult<T>();
        entity.setCode(errors.code);
        entity.setMsg(errors.msg);
        entity.setException(errors.exception);
        return entity;
    }

    public static <T> WebResult<T> fail(Errors errors, T data) {
        WebResult<T> entity = new WebResult<T>();
        entity.setCode(errors.code);
        entity.setMsg(errors.msg);
        entity.setException(errors.exception);
        entity.setData(data);
        return entity;
    }

    public static <T> WebResult<T> fail(String code, T data) {
        WebResult<T> entity = new WebResult<T>();
        entity.setCode(code);
        entity.setData(data);
        return entity;
    }

    public static <T> WebResult<T> fail(String code, String exception, String msg, T data) {
        WebResult<T> entity = new WebResult<T>();
        entity.setCode(code);
        entity.setException(exception);
        entity.setMsg(msg);
        entity.setData(data);
        return entity;
    }

    @Override
    public String toString() {
        return "WebResult{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", exception='" + exception + '\'' +
                ", data=" + data +
                ", timestamp=" + timestamp +
                '}';
    }
}
